package com.jml.mayi.controller;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class KafkaController {

	@Autowired
	private KafkaTemplate<String, String> kafkaTemplate;

	//传入的topic的名字
	@RequestMapping("/kafka")
	public String testKafka(String topic) {
		for (int i = 1; i < 100; i++) {
			send("key" + i, "data" + i,topic);
		}
		return "success";
	}
	/**
	 * 发送消息的方法
	 *
	 * @param key
	 *            推送数据的key
	 * @param data
	 *            推送数据的data
	 */
	private void send(String key, String data,String topic) {
		// topic 名称 key data 消息数据
		//kafkaTemplate.send(topic, key, data);
		kafkaTemplate.send(topic, key, data);
	}
}
